package ca.ualberta.cs.CLRDB.conference;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import uk.ac.liv.c3connector.AnnotationModel;
import ca.ualberta.cs.CLRDB.AnnotationDatabase;
import ca.ualberta.cs.CLRDB.DatabaseController;
import ca.ualberta.cs.CLRDB.interfaces.AnnotationAccess;
import ca.ualberta.cs.CLRDB.interfaces.EntityRelManagement;

/**
 * implements Proxy design pattern to control behavior/access to AnnotationDatabase for the conference scenario
 * @author samaneh
 *
 */
public class AnnotationMgmtConference extends EntityRelManagement implements AnnotationAccess {

//	private static String role = null;
	private static String msg = "role should be specified for the conference access -call setRole(String)";
	private static AnnotationDatabase annoMgmt = new AnnotationDatabase();
	
	
	@Override
	public void initialize(Statement statement) throws SQLException {
		annoMgmt.initialize(statement);
	}

	/**
	    * @param am (AnnotationModel)
	    * @param username
	    * @param pass
	    * @return  0: ok, 1: user not authenticated, 2: exception
	    */
	public int storeOrUpdateAnyAnnotation(AnnotationModel am, String username,
			String pass) {
		//authenticate, considering role

		   HashMap<String, String> auth = DatabaseController.personsMgmt.authenticate(username, pass);
		   int state = Integer.parseInt(auth.get("state"));
		   if( state != 0 )
			   return 1;
		   
		   //public anno has the username of the one authenticated with
		   //trusting the one sitting on the private computer!!!
		   if(! username.equals(am.getUserid()) ){
			   System.out.println("Warning: This annotation has a different user id than the one authenticated with! It will be published with the authenticated user id.");
			   am.setUserid(username);
		   }	   	
		
		   if(am.getResourceUri().startsWith("file:/"))
				am.setResourceUri(username+ ":" +am.getResourceUri());		
		  
		   int annotype = storeOrUpdateIfNotComment(am, username);
		   if(annotype == -1)
			   return storeOrUpdateAnnotation(am, username, pass, true);
		   /*else 
			   return annotype;*/

		resetRole();
		return annotype;
	}

	private int storeOrUpdateIfNotComment(AnnotationModel am, String username			) {		
		return annoMgmt.storeOrUpdateIfNotComment(am, username);
	}
	
	/**
	    * @param am (AnnotationModel)
	    * @param username
	    * @param pass
	    * @return  0: ok, 1: user not authenticated, 2: exception
	    */
	private int storeOrUpdateAnnotation(AnnotationModel am, String username,
			String pass, boolean hasCheckedOtherTypes) {
		if(role == null)
			return 1;
		
		if(!hasCheckedOtherTypes){
		   return storeOrUpdateAnyAnnotation(am, username, pass);		   
	    }				
		
		//get phase-role permission to store or update annotation
		boolean permit = ConferenceController.phaseMgmt.getPhaseRolePermission("addcomment", null);
		//if granted:	
		if(permit)
			annoMgmt.storeOrUpdateAnnotation(am, username, pass, true);
		
		resetRole();
		return 0;
	}

	public int deleteAnnotation(Integer ID, String username, String pass) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public ArrayList<AnnotationModel> findAnnosWithResourceUri(
			String resourceUri) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<AnnotationModel> findAnnosWithSameBib(String resourceUri) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<AnnotationModel> findAnnosWithSameMd5Base64(String checksum) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<AnnotationModel> findAnnosWithSameMultivalentBase64Md5Text(
			String checksum) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public int deleteAnnotationByFabId(String id, String username, String pass) {
		try {
			throw new Exception();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}	

}
